/**
	\file unicode_2_gb2312.c

	COPYRIGHT(C) 2011,ShenZhen smit CO.,LTD. \n
	All rights reserved. \n\n

	\brief Program start for all bcm applications.

	\details charset interface includes:

	\version 1.0
	\date 2012.6.16
	\author Wang Xiao rong
	\note Created
*/

/* Includes --------------------------------------------------------------- */

#include "stdio.h"
#include "string.h"
#include "usm_typedef.h"

#include "usm_charset.h"
#include "unicode_to_gb2312.h"

/* Private Macros --------------------------------------------------------- */

/* Private Types ---------------------------------------------------------- */

/* Private Constants ------------------------------------------------------ */

/* Private Variables (static) --------------------------------------------- */

/* Global Variables ------------------------------------------------------- */

/* Private Function prototypes -------------------------------------------- */

/* Functions -------------------------------------------------------------- */

static JZ_UINT16 GetUni2GbIndex(JZ_UCHAR unicode[])
{
	JZ_UINT16 nTmp = 0;

	nTmp = ((JZ_UINT16)unicode[1] << 8) + unicode[0];

	if (nTmp >= 0x4e00 && nTmp <= 0x9fa3)
		return uni2gbMapTable4[nTmp-0x4e00];

	else if (nTmp >= 0xa0 && nTmp <= 0x04ff)
		return uni2gbMapTable1[nTmp -0xa0];
	
	else if (nTmp >= 0x2010 && nTmp <= 0x26ff)
		return uni2gbMapTable2[nTmp -0x2010];

	else if (nTmp >= 0x3000 && nTmp <= 0x3233)
		return uni2gbMapTable3[nTmp -0x3000];

	else if (nTmp >= 0xff00 && nTmp <= 0xfff3)
		return uni2gbMapTable5[nTmp -0xff00];
	else
		return 0;

}

static JZ_INT32 ITVPVR_Unicode_a(JZ_UCHAR *pszSrc, JZ_UCHAR *pszDest, JZ_INT32 nLen)
{
	JZ_INT32 i, j;

	i = (nLen > 1) ? 2 : nLen;

	if (i < 1 ) return 0;

	if (i == 1)
	{
		pszDest[0] = pszSrc[0];
		return i;
	}
	if ((pszSrc[1] == 0) && (pszSrc[0] <= 0x7F ))
	{
		pszDest[0] = pszSrc[0];
		return 1;
	}
	else
	{
		j = GetUni2GbIndex((unsigned char*)pszSrc);

		if (j != 0)
		{
			pszDest[0] = (j / V_WEI) + OFFSET_WEI;
			pszDest[1] = (j % V_WEI) + OFFSET_WEI;
			return 2;
		}
		else
		{
			pszDest[0] = 0x20;
			return 1;
		}
	}
}

JZ_VOID ITVPVR_UnicodeToGB(JZ_UCHAR *pszSrc, JZ_UCHAR *pszDest, JZ_INT32 *plen)
{
	JZ_INT32 len, i, j, k;

	len = *plen;

	if (len == 0)
	{
		return ;
	}

	for (i = 0, j = 0; i < len; )
	{
		k = ITVPVR_Unicode_a(pszSrc + i, pszDest + j, len - i);
		i += 2;
		j += k;
	}
	*plen = j;
	pszDest[j] = 0x00;
	
	return ;
}

/**
    \fn JZ_Error_t USM_CS_Unicode2GB(JZ_CHAR *pszDest, const JZ_WCHAR *pwszSrc, JZ_INT32 nLength)

    \brief Converts a Unicode string into a GB2312 string.

    \param[out] pszDest The destination GB2312 string.
    \param[in] pwszSrc The source wide-character string.
    \param[in] nLength The length of the source wide-character string. The value of 0 represents the source wide-character string ends with a null character.

    \retval JZ_SUCCESS Successful.
    \retval JZ_FAILURE Failed.
    \retval JZ_ERROR_BAD_PARAMETER Bad parameter passed.

    \remark

    \see USM_CS_GB2Unicode
*/
JZ_Error_t USM_CS_Unicode2GB(JZ_CHAR *pszDest, const JZ_WCHAR *pwszSrc, JZ_INT32 nLength)
{
	JZ_INT32 nByte;
	
	USM_ENTER();

	USM_VERIFY(pwszSrc != JZ_NULL);
	USM_VERIFY(pszDest != JZ_NULL);
	USM_VERIFY(nLength > 0);

	nByte = nLength * 2;

	ITVPVR_UnicodeToGB((unsigned char *)pwszSrc, (unsigned char *)pszDest, &nByte);

	return(JZ_SUCCESS);
}

